<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jedrzej\Pimpable\PimpableTrait;

class Order extends Model
{
    use HasFactory;
    use PimpableTrait;


    /**
     * 表中的类型状态
     */
    // 订单类型
    public $orderTypeEnum = [
        'ordinaryOrder' => '普通订单',
        'pinkOrder' => '拼团订单',
        'seckillOrder' => '秒杀订单',
        'bargainOrder' => '砍价订单',
    ];

    //订单状态
    public $orderStatusTypeEnum = [
        'noPaid' => '未支付',
        'noDelivered' => '未发货',
        'toBeReceived' => '待收货',
        'beEvaluated' => '待评价',
        'dealDone' => '交易完成',
        'toBeWriteOff' => '待核销',
        'writtenOff' => '已核销',
        'deleted' => '已删除',
    ];

    /**
     * 强制转换字段
     */
    public $casts = [
        'orderCartInfo.cart_info' => 'array'
    ];

    //支付状态
    public $orderPayType = [
        'wechat' => '微信支付',
        'alipay' => '支付宝支付',
        'balance' => '余额支付',
        'offline' => '线下支付',
    ];

    //退款状态
    public $refundStatusTypeEnum = [
        'Refund_only' => '仅退款',
        'Refund_return' => '退款退货',
        'Refuse_refund' => '拒绝退款',
        'toBe_returned' => '商品待退货',
        'toBe_received' => '退货待收货',
        'refunded' => '已退款',
    ];

    /**
     * 强制转换字段
     */
//    public $casts = [];

    /**
     * 可搜索字段
     */
    public $searchable = [
        'type',
        'status',
        'pay_type',
        'created_at',
        'order_number',
        'user_id',
        'user_name',
        'user_phone',
        'total_num',
        'promoter_id',
        'cart_info',
        'account',
        'refund_status',
        'refund_time',
        'is_user_delete'
    ];

    /**
     * 查询字段模式
     */
    public $searchableModels = [
        'cart_info' => '%field%',
        'user_name' => '%field%',
        'product_attr_unique' => '%field%',
    ];

    /**
     * 可批量赋值字段
     */
    public $fillable = [
        'user_id',
        'mer_id',
        'cart_id',
        'store_id',
        'clerk_id',
        'seckill_id',
        'promoter_id',
        'bargain_id',
        'delivery_uid',
        'coupon_id',
        'pink_id',
        'order_number',
        'type',
        'user_name',
        'user_phone',
        'user_address',
        'freight_price',
        'total_num',
        'total_price',
        'total_postage',
        'pay_price',
        'pay_postage',
        'deduction_price',
        'coupon_price',
        'pay_time',
        'pay_type',
        'status',
        'refund_status',
        'refund_reason_wap_img',
        'refund_reason_wap_explain',
        'mark',
        'refund_reason_time',
        'refund_reason_wap',
        'refund_reason',
        'refund_price',
        'delivery_name',
        'delivery_code',
        'delivery_type',
        'delivery_id',
        'fictitious_content',
        'gain_integral',
        'use_integral',
        'back_integral',
        'unique',
        'remark',
        'cost',
        'verify_code',
        'channel_type',
        'province',
        'express_dump',
        'paid',
        'is_remind',
        'shipping_type',
        'is_mer_check',
    ];

    /**
     * 可排序字段
     */
    public $sortable = ['created_at', 'id'];

    /**
     * 默认排序字段
     */
    protected $defaultSortCriteria = ['id,desc'];


    /**
     * 订单所属用户
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    /**
     * 订单所属用户
     */
    public function promoter(){
        return $this->belongsTo(User::class,'promoter_id','id');
    }

    /**
     * 订单关联的购物车
     */
    function cart()
    {
        return $this->belongsTo(Cart::class, 'cart_id', 'id');
    }

    /**
     * 订单的所有操作记录
     */
    function orderLog()
    {
        return $this->hasMany(OrderLog::class, 'order_id', 'id');
    }

    /**
     * 订单的购物车信息
     */
    function orderCartInfo(){
        return $this->hasOne(OrderCartInfo::class,'order_id','id');
    }

    /**
     * 订单使用优惠
     */
    function orderEconomize()
    {
        return $this->hasOne(OrderEconomize::class, 'order_id', 'id');
    }

    /**
     * 订单关联的优惠券
     */
    function coupon()
    {
        return $this->belongsTo(StoreCouponIssue::class, 'coupon_id', 'id');
    }

    /**
     * 订单关联的配送员
     */
    function delivery()
    {
        return $this->belongsTo(DeliveryService::class, 'delivery_id', 'id');
    }

    /**
     * 订单所属的商户
     */
    function mer()
    {
        return $this->belongsTo(MerchantRegister::class, 'mer_id', 'id');
    }

    /**
     * 订单关联的拼团
     */
    function combination()
    {
        return $this->belongsTo(StoreCombination::class, 'combination_id', 'id');
    }

    /**
     * 订单关联的秒杀
     */
    function seckill()
    {
        return $this->belongsTo(StoreSeckill::class, 'seckill_id', 'id');
    }

    /**
     * 订单关联的砍价
     */
    function bargain()
    {
        return $this->belongsTo(StoreBargain::class, 'bargain_id', 'id');
    }

    /**
     * 订单关联的门店
     */
    function store()
    {
        return $this->belongsTo(SystemStore::class, 'store_id', 'id');
    }

    /**
     * 订单关联的店员
     */
    function clerk()
    {
        return $this->belongsTo(SystemStoreStaff::class, 'clerk_id', 'id');
    }


}
